﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Model;
using System.Net.Http;

namespace EasyUIDemo.MVC.Controllers
{
    public class Common
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="keyDefault"></param>
        /// <param name="dictionarytype"></param>
        /// <param name="valueDefault"></param>
        /// <returns></returns>
        public static dynamic EnumListDic(string keyDefault, string dictionarytype, string valueDefault = "")
        {
            List<dynamic> dicEnum = new List<dynamic>();

            if (!string.IsNullOrEmpty(keyDefault)) //判断是否添加默认选项  
            {
                dicEnum.Add(new { value = valueDefault, text = keyDefault });
            }

            var list = (List<Dictionary>)HttpContext.Current.Session["dictionary"];
            list = list.Where(n => n.dictionary_Type == dictionarytype).ToList();
            foreach (var item in list)
            {
                dicEnum.Add(new { value = (int)(item.Dictionary_IdNumber), text = item.dictionary_Name });  //不用枚举的value值作为字典key值的原因从枚举例子能看出来，其实这边应该判断他的值不存在，默认取字段名称  
            }
            return dicEnum;
        }
        /// <summary>
        /// 转成集合
        /// </summary>
        /// <param name="keyDefault"></param>
        /// <param name="valueDefault"></param>
        /// <returns></returns>
        public static Dictionary<string, object> EnumListDic(string keyDefault, string valueDefault = "")
        {
            Dictionary<string, object> dicEnum = new Dictionary<string, object>();

            if (!string.IsNullOrEmpty(keyDefault)) //判断是否添加默认选项  
            {
                dicEnum.Add(keyDefault, valueDefault);
            }

            var list = (List<Dictionary>)HttpContext.Current.Session["dictionary"];
            list = list.Where(n => n.dictionary_ParentId == 1).ToList();
            foreach (var item in list)
            {
                dicEnum.Add(item.dictionary_Type, item.dictionary_Type);  //不用枚举的value值作为字典key值的原因从枚举例子能看出来，其实这边应该判断他的值不存在，默认取字段名称  
            }
            return dicEnum;
        }
        /// <summary>
        /// 获取字典的数据
        /// </summary>
        /// <returns></returns>
        public static List<Dictionary> list()
        {
            List<Dictionary> lists = null;
            using (HttpClient client = new HttpClient())
            {
                string uri = "http://localhost:63805/apis/Dictionary/getdictionary";
                var result = client.GetAsync(uri).Result;
                if (result.IsSuccessStatusCode)
                {
                    var res = result.Content.ReadAsStringAsync().Result;
                    lists = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Dictionary>>(res);
                    
                }
                HttpContext.Current.Session["dictionary"] = lists;
                return lists;
            }
        }
    }
}